/*
 * Copyright 2010 Dovado FZ-LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dovado.dovadoremote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.ImageView;

public class SplashScreen extends Activity {
	protected boolean _active = true;
	protected int _splashTime = 5000;
	private static boolean hasBeenDisplayed = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (hasBeenDisplayed) {
			finish();

			// startActivity(new Intent("com.dovado.dovadoremote.DAAPP"));
		}
		super.onCreate(savedInstanceState);

		setContentView(R.layout.splashscreen);

		/*
		 * show picture
		 */

		ImageView i = (ImageView) findViewById(R.id.splashpicture);
		i.setImageResource(R.drawable.splash);
		// thread for displaying the SplashScreen
		Thread splashTread = new Thread() {
			@Override
			public void run() {
				try {
					int waited = 0;
					while (_active && (waited < _splashTime)) {
						sleep(100);
						if (_active) {
							waited += 100;
						}
					}
				} catch (InterruptedException e) {
					// do nothing
				} finally {
					/*
					 * Go back to the tabs (which will display settings) after
					 * the splash:
					 */
					hasBeenDisplayed = true;
					finish();
					startActivity(new Intent("com.dovado.dovadoremote.DAAPP"));
				}
			}
		};
		splashTread.start();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Don't allow the user to press any keys while splashing
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			_active = false;
		}
		return true;
	}
}